package com.mrjy.common.thread;

import com.mrjy.common.util.ConstantsUtil;
import com.mrjy.wechat.conts.WxConsts;
import com.mrjy.wechat.util.WeChatUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;

/**
 * Created by lyon on 2018/5/17 0017.
 */
public class ThreadTask implements Runnable, Cloneable {
    private static Logger logger = Logger.getLogger(ThreadTask.class);
    private static ThreadTask threadTask = new ThreadTask();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    private Integer videoId;
    private Integer coursePackageId;
    private String videoTypes;
    private String teachers;
    private String releaseTimeString;
    private String videoName;
    private String coursePackageName;
    private String userName;
    private String weixinOpenid;
    private String updateTimeString;
    private String videoContent;
    private String duringTime;
    private int type; // 微课 ：1 课程包：2 活动：3
    private String first ;
    private String remark ;

    public ThreadTask(Integer videoId,
                      Integer coursePackageId, String videoTypes, String teachers,
                      String releaseTimeString, String videoName,
                      String coursePackageName, String userName, String weixinOpenid,
                      String updateTimeString, int type,String first,String remark) {
        super();
		this.videoId = videoId;
		this.coursePackageId = coursePackageId;
		this.videoTypes = videoTypes;
		this.teachers = teachers;
		this.releaseTimeString = releaseTimeString;
		this.videoName = videoName;
		this.coursePackageName = coursePackageName;
		this.userName = userName;
		this.weixinOpenid = weixinOpenid;
		this.updateTimeString = updateTimeString;
        this.type = type;
        this.first = first ;
		this.remark = remark ;
    }
    

	private ThreadTask() {
    }

    @Override
    public void run() {
        try {
        	//微课通知
            if (type == 1) {
            	//int length = releaseTimeString.length();
                //releaseTimeString = releaseTimeString.substring(0, length - 3);
                if(StringUtils.isBlank(first)){
                	first = ConstantsUtil.getProperty("VIDEO.NOTICE_TITLE_DESC");
                }
                if(StringUtils.isBlank(remark)){
                	remark = ConstantsUtil.getProperty("VIDEO.NOTICE_FOOT_DESC") ;
                }
                WeChatUtil.sendWechatTemplateMessageCourseRelease(first, remark, videoName, videoTypes, teachers, updateTimeString,
                        WxConsts.PUBLIC_DOMAIN + "/index.html?from=weClasses/course_detail.html@_@vid=" + videoId, weixinOpenid);
            } else if (type == 2) {//课程包通知
            	int length = releaseTimeString.length();
                releaseTimeString = releaseTimeString.substring(0, length - 3);
                
                if (StringUtils.isNotBlank(updateTimeString)) {//课程包上新
                    WeChatUtil.sendWechatTemplateMessageCourseRelease("你报名参加的课程更新了", "你主动报名参加的课程已有数万家长在学，请一起参与哦！", coursePackageName, videoTypes, teachers
                            , updateTimeString, WxConsts.PUBLIC_DOMAIN + "/index.html?from=weCoursePackage/themeP_detail.html@_@id=" + coursePackageId, weixinOpenid);
                } else {//课程包上架
                    WeChatUtil.sendWechatTemplateMessageCourseRelease("家庭教育实战型专家精心为你打造的系列课程，现在上架啦！", "家长好好学习，孩子天天向上。为了一家人的幸福，现在可享受限时优惠报名哦！", coursePackageName, videoTypes, teachers
                            , releaseTimeString, WxConsts.PUBLIC_DOMAIN + "/index.html?from=weCoursePackage/themeP_detail.html@_@id=" + coursePackageId, weixinOpenid);
                }
            } else if (type == 3) {
                //活动
            	WeChatUtil.sendWechatTemplateMessageCourseRelease(first, remark, coursePackageName, videoTypes, teachers
                        , updateTimeString, WxConsts.PUBLIC_DOMAIN + "/index.html?from=weQA/active_detail.html@_@activeId=" + videoId, weixinOpenid);
            }

        } catch (Exception e) {
            logger.fatal("消息发送失败：" + videoId + ",weixinOpenid:" + weixinOpenid);
            e.printStackTrace();
        }
    }

    public static ThreadTask clonePolyInfo(Integer videoId,
                                           Integer coursePackageId, String videoTypes, String teachers,
                                           String releaseTimeString, String videoName,
                                           String coursePackageName, String userName, String weixinOpenid,
                                           String updateTimeString, int type,String first
                                           ,String remark) throws CloneNotSupportedException {
        ThreadTask child_threadTask = (ThreadTask) threadTask.clone();
        child_threadTask.setReleaseTimeString(releaseTimeString);
        child_threadTask.setVideoId(videoId);
        child_threadTask.setVideoName(videoName);
        child_threadTask.setUserName(userName);
        child_threadTask.setWeixinOpenid(weixinOpenid);
        child_threadTask.setCoursePackageId(coursePackageId);
        child_threadTask.setCoursePackageName(coursePackageName);
        child_threadTask.setVideoTypes(videoTypes);
        child_threadTask.setTeachers(teachers);
        child_threadTask.setUpdateTimeString(updateTimeString);
        child_threadTask.setType(type);
        child_threadTask.setFirst(first);
        child_threadTask.setRemark(remark);
        return child_threadTask;
    }

    public Integer getVideoId() {
        return videoId;
    }

    public void setVideoId(Integer videoId) {
        this.videoId = videoId;
    }

    public String getReleaseTimeString() {
        return releaseTimeString;
    }

    public void setReleaseTimeString(String releaseTimeString) {
        this.releaseTimeString = releaseTimeString;
    }

    public String getVideoName() {
        return videoName;
    }

    public void setVideoName(String videoName) {
        this.videoName = videoName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getWeixinOpenid() {
        return weixinOpenid;
    }

    public void setWeixinOpenid(String weixinOpenid) {
        this.weixinOpenid = weixinOpenid;
    }

	public Integer getCoursePackageId() {
		return coursePackageId;
	}

	public void setCoursePackageId(Integer coursePackageId) {
		this.coursePackageId = coursePackageId;
	}

	public String getVideoTypes() {
		return videoTypes;
	}

	public void setVideoTypes(String videoTypes) {
		this.videoTypes = videoTypes;
	}

	public String getTeachers() {
		return teachers;
	}

	public void setTeachers(String teachers) {
		this.teachers = teachers;
	}

	public String getCoursePackageName() {
		return coursePackageName;
	}

	public void setCoursePackageName(String coursePackageName) {
		this.coursePackageName = coursePackageName;
	}

	public String getUpdateTimeString() {
		return updateTimeString;
	}

	public void setUpdateTimeString(String updateTimeString) {
		this.updateTimeString = updateTimeString;
	}

    public String getDuringTime() {
        return duringTime;
    }

    public void setDuringTime(String duringTime) {
        this.duringTime = duringTime;
    }

    public String getVideoContent() {
        return videoContent;
    }

    public void setVideoContent(String videoContent) {
        this.videoContent = videoContent;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

	public String getFirst() {
		return first;
	}

	public void setFirst(String first) {
		this.first = first;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
    
}